package dao.impl;



import dao.UserDao;

import pojo.Users;

import java.sql.SQLException;

import java.util.ArrayList;

public class UserDaoImpl extends BaseDao implements UserDao {


    //获取读者信息
    @Override
    public ArrayList<Users> getUserList() {
        ArrayList<Users> usersList = new ArrayList<Users>();
        Users users = null;

        //建立数据库连接
        this.conn = this.getConnection();

        try {
            this.state = this.conn.createStatement();
            this.rs = this.state.executeQuery("Select * from user");

            while (rs.next()) {
                users = new Users();
                users.setuID(rs.getInt("uID"));
                users.setuName(rs.getString("uName"));
                users.setuEmail(rs.getString("uEmail"));
                users.setuPhone(rs.getString("uPhone"));
                users.setMax_num(rs.getInt("max_num"));

                usersList.add(users);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return usersList;
    }

    //通过ID删除读者
    @Override
    public int delUserbyID(int uID) {
        int res = 0;
        // 建立连接
        this.conn = this.getConnection();
        try {
            this.pst =this.conn.prepareStatement("DELETE FROM user where uID = ? ");
            pst.setInt(1,uID);

            res = pst.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    //更新读者
    @Override
    public int updateuser(Users users) {
        String strSQl= "UPDATE user " +
                "SET  uName = ? " +
                ",uEmail = ?" +
                ",uPhone = ?" +
                ",max_num = ?  WHERE  uID = ?";
        int res = 0;
        // 建立连接
        this.conn = this.getConnection();
        try {
            this.pst =this.conn.prepareStatement(strSQl);
            pst.setString(1,users.getuName());
            pst.setString(2,users.getuEmail());
            pst.setString(3,users.getuPhone());
            pst.setInt(4,users.getMax_num());

            res = pst.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    //添加读者
    @Override
    public int adduser(Users users) {
        String strSQl= "INSERT  INTO user(uID,uName,uEmail,uPhone,max_num)" +
                "VALUES (?,?,?,?,?)";
        int res = 0;
        // 建立连接
        this.conn = this.getConnection();
        try {
            this.pst =this.conn.prepareStatement(strSQl);
            pst.setInt(1,users.getuID());
            pst.setString(2,users.getuName());
            pst.setString(3,users.getuEmail());
            pst.setString(4,users.getuPhone());
            pst.setInt(5,users.getMax_num());

            res = pst.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }
}
